from datetime import datetime
from fastapi import HTTPException
from pydantic import BaseModel, Field, field_validator, model_validator


# 定义就业基类
class JobModel(BaseModel):
    stu_id_id: int
    open_time: str
    offer_time: str
    company_name: str
    salary: int

    #  含时间字段的验证模型
    @field_validator('open_time')
    def validate_open_time(cls, open_time):
        try:
            datetime.strptime(open_time, "%Y-%m-%d")
        except ValueError as e:
            raise ValueError("日期不合法，请检查格式（应为YYYY-MM-DD）") from e
        else:
            return open_time

    @field_validator('offer_time')
    def validate_offer_time(cls, offer_time):
        try:
            datetime.strptime(offer_time, "%Y-%m-%d")
        except ValueError as e:
            raise ValueError("日期不合法，请检查格式（应为YYYY-MM-DD）") from e
        else:
            return offer_time

    # 跨字段验证
    @model_validator(mode="after")  # mode="after"表示在所有字段验证完成后执行
    def validate_time_order(self):
        # 将字符串转换为datetime对象进行比较
        open_time = datetime.strptime(self.open_time, "%Y-%m-%d")
        offer_time = datetime.strptime(self.offer_time, "%Y-%m-%d")
        if offer_time < open_time:
            raise ValueError("offer_time必须大于等于open_time")
        return self  # 验证通过，返回模型实例

    # 工资必须大于0验证
    @field_validator("salary")
    def validate_age(cls, salary):
        assert salary >= 0, "输入的参数范围不合理"
        return salary


class StudentModel(BaseModel):
    stu_id: int
    stu_class_name: str
    stu_name: str
    stu_jiguan: str
    stu_university: str
    stu_major: str
    stu_start_time: str
    stu_end_time: str
    stu_major_level: str
    stu_hr_id: int
    stu_age: int
    stu_gender: str

    @field_validator("stu_start_time")
    def validate_start_time(cls, stu_start_time):
        try:
            datetime.strptime(stu_start_time, "%Y-%m-%d")
        except ValueError as e:
            raise ValueError("日期不合法，请检查格式（应为YYYY-MM-DD）") from e
        else:
            return stu_start_time




    @field_validator("stu_end_time")
    def validate_end_time(cls, stu_end_time):
        try:
            datetime.strptime(stu_end_time, "%Y-%m-%d")
        except ValueError as e:
            raise ValueError("日期不合法，请检查格式（应为YYYY-MM-DD）") from e
        else:
            return stu_end_time

    @model_validator(mode="after")  # mode="after"表示在所有字段验证完成后执行
    def validate_time_order(self):
        # 将字符串转换为datetime对象进行比较
        start_time = datetime.strptime(self.stu_start_time, "%Y-%m-%d")
        end_time = datetime.strptime(self.stu_end_time, "%Y-%m-%d")

        if start_time >= end_time:
            raise ValueError("开始时间必须早于结束时间")
        return self  # 验证通过，返回模型实例

    @field_validator("stu_age")
    def validate_age(cls, stu_age):
        assert 0 < stu_age < 120, "输入的参数范围不合理"
        return stu_age

    @field_validator("stu_gender")
    def validate_gender(cls, stu_gender):
        assert stu_gender in ["男", "女"], "输入有误,请重新输入"
        return stu_gender


# 增加学生校验
class Add_score(BaseModel):
    s_id: int
    test_time: int
    stu_grade: float = Field(..., le=100,ge=0)
    # @field_validator("stu_grade")
    # def stu_grade_jiaoyan(cls, value):
    #     if not (0 <= value <= 100):
    #         raise HTTPException(
    #             status_code=404,
    #             detail="输入的分数不在范围内"
    #         )
    #     return value


# 更新学生成绩模型校验
class Student_score_put(BaseModel):
    test_time: int = 1
    stu_grade: float

    @field_validator("stu_grade")
    def validate_stu_grade(cls, value):
        if not (0 <= value <= 100):
            raise HTTPException(
                status_code=404,
                detail="分数不在此区间呢"
            )
        return value


# 删除学生成绩模型校验
class Student_score_delete(BaseModel):
    s_id: int
    test_time: int = 1




















